import util.StockUtils

class StockController {

    def index = { 
        render(view:"stockInformation")
    }

    def stockInformation = {
    }

    def doStockInformation = {
        def stockCode = params.stockCode
        def stocks = StockUtils.getStockInformation(stockCode)
        def resultList = new ArrayList()
        for (String stock : stocks) {
            String[] datas = stock.split(",");
            if (datas.length == 1) break;
            StockDTO stockDTO = new StockDTO()
            stockDTO.setStockName(datas[0].substring(21))
            stockDTO.setTime(datas[30] + " "+datas[31].substring(0,8))
            stockDTO.setOpen(datas[1])
            stockDTO.setYesterday(datas[2])
            stockDTO.setNow(datas[3])
            stockDTO.setHigh(datas[4])
            stockDTO.setLow(datas[5])
            def total = datas[8].toInteger()/100
            stockDTO.setTotal(total.toString())
            stockDTO.setMoney(datas[9])
            resultList.add(stockDTO)
        }
        render(view:"stockInformation",model:[stocks:resultList])
    }

    def showDailyK = {
        def stockCode = params.id
        response.setContentType("image/gif");
        OutputStream out = response.getOutputStream();
        
        def image= StockUtils.getDailyK(stockCode)
        byte[] bytes=image.toByteArray()
        out.write(bytes)
        out.close()
    }
}
